#include "BondDetailChart.h"
#include "BondDetailData.h"
#include "BondDetailChartTitle.h"
#include "../BondUtility.h"
#include "./BondChart/bondratechart.h"
#include "./BondChart/bondinterestratechart.h"
#include "./BondChart/bondquotedealchart.h"
#include "./BondChart/bondquotebrokerchart.h"
#include "./BondChart/bondquotecdcchart.h"
#include "./BondChart/bondcharttools.h"
#include <core/Utility.h>
#include <bondlib/BondContainer.h>
#include <uam/ServerTimeMgr.h>
#include <uam/UserAccountManager.h>
#include <widgets/DualCalendars.h>
#include <QMouseEvent>
#include <QStylePainter>
#include <QtWidgets/QGridLayout>

#define CHART_BOND_QUOTE_LINE_HEIGHT 32

BondDetailChart::BondDetailChart(QWidget* parent)
    : QWidget(parent)
{
	setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}

BondDetailChart::~BondDetailChart()
{

}

void BondDetailChart::setDetailData(QSharedPointer<DetailData> data)
{
    m_data=data;
    setupUi();
}

static void showLayout(QLayout* l, bool visible = true)
{
	int count = l->count();
	for (int i = 0; i < count; ++i)
	{
		QLayoutItem* li = l->itemAt(i);
		if (li->widget())
			li->widget()->setVisible(visible);
		else if (li->layout())
		{
			showLayout(li->layout());
		}
	}
}

void BondDetailChart::setupUi()
{
	for (int i = 0; i < BONDDETAILCHART_COUNT; i++)
	{
		mDetailLayout[i] = new QVBoxLayout();
		mDetailLayout[i]->setContentsMargins(0, 0, 0, 0);
		mDetailLayout[i]->setSpacing(0);
	}

	mBondDetailDealTitle = new BondDetailDealChartTitle(this);
	mBondDetailQuoteTitle = new BondDetailQuoteChartTitle(this);
	mBondDetailRateTitle = new BondDetailRateChartTitle(this);
	mBondDetailCDCTitle = new BondDetailCDCChartTitle(this);

	mBondDetailDealTitle->setFixedHeight(CHART_BOND_QUOTE_LINE_HEIGHT * 2);
	mBondDetailQuoteTitle->setFixedHeight(CHART_BOND_QUOTE_LINE_HEIGHT * 2);
	mBondDetailRateTitle->setFixedHeight(CHART_BOND_QUOTE_LINE_HEIGHT * 2);
	mBondDetailCDCTitle->setFixedHeight(CHART_BOND_QUOTE_LINE_HEIGHT * 2);

	mDetailLayout[0]->addWidget(mBondDetailDealTitle);
	mDetailLayout[1]->addWidget(mBondDetailQuoteTitle);
	mDetailLayout[2]->addWidget(mBondDetailRateTitle);
	mDetailLayout[3]->addWidget(mBondDetailCDCTitle);

	mBondQuoteDealChart = new BondQuoteDealChart(this);
	mBondQuoteBrokerChart = new BondQuoteBrokerChart(this);
	mBondRateChart = new BondRateChart(this);
	mBondInterestRateChart = new BondInterestRateChart(this);
	mBondQuoteCDCChart = new BondQuoteCDCChart(this);

	// big/small chart
	connect(mBondQuoteDealChart, &BondQuoteDealChart::mouseDoubleClick, this, &BondDetailChart::onChartDblClicked);
	connect(mBondQuoteBrokerChart, &BondQuoteBrokerChart::mouseDoubleClick, this, &BondDetailChart::onChartDblClicked);
	connect(mBondRateChart, &BondRateChart::mouseDoubleClick, this, &BondDetailChart::onChartDblClicked);
	connect(mBondInterestRateChart, &BondInterestRateChart::mouseDoubleClick, this, &BondDetailChart::onChartDblClicked);
	connect(mBondQuoteCDCChart, &BondQuoteCDCChart::mouseDoubleClick, this, &BondDetailChart::onChartDblClicked);

	mDetailLayout[0]->addWidget(mBondQuoteDealChart);
	mDetailLayout[1]->addWidget(mBondQuoteBrokerChart);
	mDetailLayout[2]->addWidget(mBondRateChart);
	mDetailLayout[3]->addWidget(mBondQuoteCDCChart);

	QGridLayout* gridLayout = new QGridLayout();
	gridLayout->setContentsMargins(0, 0, 0, 0);
	gridLayout->setSpacing(4);
	gridLayout->addLayout(mDetailLayout[0], 0, 0);
	gridLayout->addLayout(mDetailLayout[1], 0, 1);
	gridLayout->addLayout(mDetailLayout[2], 1, 0);
	gridLayout->addLayout(mDetailLayout[3], 1, 1);
	setLayout(gridLayout);

	// data
    connect(m_data.get(), &DetailData::bondChanged, this, &BondDetailChart::onBondChanged);
    connect(m_data.get(), &DetailData::dealChartKLineChanged, this, &BondDetailChart::onDealChartKLineChanged);
    connect(m_data.get(), &DetailData::dealChartMarketDealChanged, this, &BondDetailChart::onDealChartMarketDealChanged);
    connect(m_data.get(), &DetailData::hisPriceChanged, this, &BondDetailChart::onDealChartPreCloseChanged);
    connect(m_data.get(), &DetailData::rateChartChanged, this, &BondDetailChart::onBondRateChanged);
    connect(m_data.get(), &DetailData::interestChartChanged, this, &BondDetailChart::onBondInterestChanged);
    connect(m_data.get(), &DetailData::cdcChartChanged, this, &BondDetailChart::onBondCDCChanged);
    connect(m_data.get(), &DetailData::marketStreamChanged, this, &BondDetailChart::onMarketStreamDataChanged);
    connect(m_data.get(), &DetailData::cfetsMarketStreamChanged, this, &BondDetailChart::onMarketStreamDataChanged);

	// ui control
	connect(mBondDetailDealTitle, &BondDetailDealChartTitle::realTimeTabSelected, this, &BondDetailChart::onBondDealTabSelRealTime);
	connect(mBondDetailDealTitle, &BondDetailDealChartTitle::dayAverTabSelected, this, &BondDetailChart::onBondDealTabSelKLineAver);
	connect(mBondDetailDealTitle, &BondDetailDealChartTitle::dayClosedTabSelected, this, &BondDetailChart::onBondDealTabSelKLineClose);
	connect(mBondDetailDealTitle, &BondDetailDealChartTitle::dateTabSelected, this, &BondDetailChart::onBondDealTabSelDate);
	connect(mBondDetailQuoteTitle, &BondDetailQuoteChartTitle::yieldSelected, this, &BondDetailChart::onQuoteYieldSelected);
	connect(mBondDetailQuoteTitle, &BondDetailQuoteChartTitle::netPriceSelected, this, &BondDetailChart::onQuoteNetPriceSelected);
	connect(mBondDetailQuoteTitle, &BondDetailQuoteChartTitle::filterAbValueSelected, this, &BondDetailChart::onQuoteAbValueSelected);
	connect(mBondDetailRateTitle, &BondDetailRateChartTitle::creditSpreadSelected, this, &BondDetailChart::onBondCreditSpreadSelected);
	connect(mBondDetailRateTitle, &BondDetailRateChartTitle::yieldSelected, this, &BondDetailChart::onBondYieldSelected);
	connect(mBondDetailCDCTitle, &BondDetailCDCChartTitle::yieldTabSelected, this, &BondDetailChart::onBondCDCYieldSelected);
	connect(mBondDetailCDCTitle, &BondDetailCDCChartTitle::netPriceTabSelected, this, &BondDetailChart::onBondCDCNetPriceSelected);
	connect(mBondDetailCDCTitle, &BondDetailCDCChartTitle::dateTabSelected, this, &BondDetailChart::onBondCDCDateTabSelected);

	connect(mBondDetailDealTitle->getBrokerDualCalendars(), &DualCalendars::StartEndDatedSelected, this, &BondDetailChart::onBondDealCalendarUpdate);
	connect(mBondDetailDealTitle->getCFETSDualCalendars(), &DualCalendars::StartEndDatedSelected, this, &BondDetailChart::onBondDealCalendarUpdate);
	connect(mBondDetailCDCTitle->getBrokerDualCalendars(), &DualCalendars::StartEndDatedSelected, this, &BondDetailChart::onBondCDCCalendarUpdate);
	connect(mBondDetailCDCTitle->getCFETSDualCalendars(), &DualCalendars::StartEndDatedSelected, this, &BondDetailChart::onBondCDCCalendarUpdate);

	connect(mBondQuoteDealChart, &BondQuoteDealChart::calendarChanged, mBondDetailDealTitle, &BondDetailDealChartTitle::onDualCalendarsGetDate);
	connect(mBondQuoteCDCChart, &BondQuoteCDCChart::calendarChanged, mBondDetailCDCTitle, &BondDetailCDCChartTitle::onDualCalendarsGetDate);

	initBondQuoteBorker();
	initBondQuoteDeal();
	initBondRate();
	initBondInterestRate();
	initBondQuoteCDC();

	// date drag
	connect(mBondQuoteDealChart->dragBar(), &BaseChartDragBarElement::leftDrag, this, &BondDetailChart::onBondDealLeftDrag);
	connect(mBondQuoteDealChart->dragBar(), &BaseChartDragBarElement::rightDrag, this, &BondDetailChart::onBondDealRightDrag);
	connect(mBondQuoteDealChart->dragBar(), &BaseChartDragBarElement::centralDrag, this, &BondDetailChart::onBondDealCentralDrag);
	connect(mBondQuoteBrokerChart->dragBar(), &BaseChartDragBarElement::leftDrag, this, &BondDetailChart::onBondQuoteLeftDrag);
	connect(mBondQuoteBrokerChart->dragBar(), &BaseChartDragBarElement::rightDrag, this, &BondDetailChart::onBondQuoteRightDrag);
	connect(mBondQuoteBrokerChart->dragBar(), &BaseChartDragBarElement::centralDrag, this, &BondDetailChart::onBondQuoteCentralDrag);
	connect(mBondRateChart->dragBar(), &BaseChartDragBarElement::leftDrag, this, &BondDetailChart::onBondRateLeftDrag);
	connect(mBondRateChart->dragBar(), &BaseChartDragBarElement::rightDrag, this, &BondDetailChart::onBondRateRightDrag);
	connect(mBondRateChart->dragBar(), &BaseChartDragBarElement::centralDrag, this, &BondDetailChart::onBondRateCentralDrag);
	connect(mBondInterestRateChart->dragBar(), &BaseChartDragBarElement::leftDrag, this, &BondDetailChart::onBondInterestLeftDrag);
	connect(mBondInterestRateChart->dragBar(), &BaseChartDragBarElement::rightDrag, this, &BondDetailChart::onBondInterestRightDrag);
	connect(mBondInterestRateChart->dragBar(), &BaseChartDragBarElement::centralDrag, this, &BondDetailChart::onBondInterestCentralDrag);
	connect(mBondQuoteCDCChart->dragBar(), &BaseChartDragBarElement::leftDrag, this, &BondDetailChart::onBondCDCLeftDrag);
	connect(mBondQuoteCDCChart->dragBar(), &BaseChartDragBarElement::rightDrag, this, &BondDetailChart::onBondCDCRightDrag);
	connect(mBondQuoteCDCChart->dragBar(), &BaseChartDragBarElement::centralDrag, this, &BondDetailChart::onBondCDCCentralDrag);
	connect(mBondQuoteDealChart->dragBar(), &BaseChartDragBarElement::leftDrag, mBondDetailDealTitle, &BondDetailDealChartTitle::onBondDateBtnChanged);
	connect(mBondQuoteDealChart->dragBar(), &BaseChartDragBarElement::rightDrag, mBondDetailDealTitle, &BondDetailDealChartTitle::onBondDateBtnChanged);
	connect(mBondQuoteCDCChart->dragBar(), &BaseChartDragBarElement::leftDrag, mBondDetailCDCTitle, &BondDetailCDCChartTitle::onBondDateBtnChanged);
	connect(mBondQuoteCDCChart->dragBar(), &BaseChartDragBarElement::rightDrag, mBondDetailCDCTitle, &BondDetailCDCChartTitle::onBondDateBtnChanged);
}

void BondDetailChart::initBondQuoteBorker()
{
	QB_CHECK_RETURN_VOID1(mBondQuoteBrokerChart);

	mBondQuoteBrokerChart->setPriceType(BondQuoteBrokerChart::prc_Yeild);
	mBondQuoteBrokerChart->setSelect(1);
	mBondQuoteBrokerChart->createChart();
}

void BondDetailChart::initBondQuoteDeal()
{
	QB_CHECK_RETURN_VOID1(mBondQuoteDealChart);

	mBondQuoteDealChart->setSelect(1);
	mBondQuoteDealChart->setHotPosSize(1);
	mBondQuoteDealChart->createChart();
}

void BondDetailChart::initBondRate()
{
	QB_CHECK_RETURN_VOID1(mBondRateChart);

	mBondRateChart->setType(BondRateChart::type_All);
	mBondRateChart->createChart();
}

void BondDetailChart::initBondInterestRate()
{
	QB_CHECK_RETURN_VOID1(mBondInterestRateChart);

	mBondInterestRateChart->setVisible(false);
	mBondInterestRateChart->createChart();
}

void BondDetailChart::initBondQuoteCDC()
{
	QB_CHECK_RETURN_VOID1(mBondQuoteCDCChart);

	mBondQuoteCDCChart->setTab(0);
	mBondQuoteCDCChart->setCheck(0, 0);
	mBondQuoteCDCChart->setCheck(1, 0);//成交
	//mBondQuoteCDCChart->setCheck(2, 0);//报价
	mBondQuoteCDCChart->createChart();
	onBondCDCChanged();
}

void BondDetailChart::onBondChanged()
{
	QB_CHECK_RETURN_VOID1(m_data && mBondQuoteBrokerChart && mBondQuoteDealChart && mBondRateChart && mBondInterestRateChart && mBondQuoteCDCChart
	&& mBondDetailDealTitle);

	mBondDetailDealTitle->setBondInfo(m_data);
	mBondDetailQuoteTitle->setBondInfo(m_data);
	mBondDetailRateTitle->setBondInfo(m_data);
	mBondDetailCDCTitle->setBondInfo(m_data);

	mBondQuoteBrokerChart->setBondInfo(&m_data->bond());
	mBondQuoteDealChart->setBondInfo(&m_data->bond());
	mBondRateChart->setBondInfo(&m_data->bond());
	mBondInterestRateChart->setBondInfo(&m_data->bond());
	mBondQuoteCDCChart->setBondInfo(&m_data->bond());

	refreshMarketDealChart();
	refreshYeildAndIntChart();

	mBondDetailDealTitle->notify();
	mBondDetailQuoteTitle->notify();
	mBondDetailRateTitle->notify();
	mBondDetailCDCTitle->notify();

}

void BondDetailChart::onDealChartKLineChanged(int type)
{
	QB_CHECK_RETURN_VOID1(m_data);

	switch ((em_NewDetail)type)
	{
	case detailModel_DealCurveKLineCFETS:
	{
		mBondQuoteDealChart->loadKLineData(BondDetailCache::instance().getChart(m_data->bond().GetCombBondKey()).m_pDealCurveKLineCFETS);
		onBondCDCChanged();
	}
	break;
	case detailModel_DealCurveKLineEx:
	{
		mBondQuoteDealChart->loadKLineData(BondDetailCache::instance().getChart(m_data->bond().GetCombBondKey()).m_pDealCurveKLineEx);
		onBondCDCChanged();
	}
	break;
	case detailModel_DealCurveKLineBroker:
	{
		mBondQuoteDealChart->loadKLineData(BondDetailCache::instance().getChart(m_data->bond().GetCombBondKey()).m_pDealCurveKLineBroker);
		onBondCDCChanged();
	}
	break;
	case detailModel_DealCurveKLineCloseEx:
	{
		mBondQuoteDealChart->loadKLineCloseData(BondDetailCache::instance().getChart(m_data->bond().GetCombBondKey()).m_pDealKlineDayCloseEx);
		onBondCDCChanged();
	}
	break;
	case detailModel_DealCurveKLineCloseCFETS:
	{
		mBondQuoteDealChart->loadKLineCloseData(BondDetailCache::instance().getChart(m_data->bond().GetCombBondKey()).m_pCfetsDealKlineCfetsDayClose);
		onBondCDCChanged();
	}
	break;
	case detailModel_DealCurveKLineCloseBroker:
	{
		mBondQuoteDealChart->loadKLineCloseData(BondDetailCache::instance().getChart(m_data->bond().GetCombBondKey()).m_pBrokerDealKlineBrokerDayClose);
		onBondCDCChanged();
	}
	break;
	case detailModel_DealCurveKLineCloseQuote:
	{
		onBondCDCChanged();
	}
	break;
	default:
	break;
	}
}

void BondDetailChart::onDealChartMarketDealChanged()
{
	refreshMarketDealChart();
}

void BondDetailChart::onDealChartPreCloseChanged()
{
	QB_CHECK_RETURN_VOID1(m_data && mBondQuoteDealChart);

	mBondQuoteDealChart->setPreClose(m_data->dealPreClose().c_str());
}

void BondDetailChart::onBondRateChanged()
{
	QB_CHECK_RETURN_VOID1(m_data && mBondRateChart);

	mBondRateChart->loadData(BondDetailCache::instance().getChart(m_data->bond().GetCombBondKey()).m_pYieldCurve, m_data->bond().GetRemainedDays(ServerTimeMgr::instance().serverTime()));
}

void BondDetailChart::onBondInterestChanged()
{
	QB_CHECK_RETURN_VOID1(m_data && mBondInterestRateChart && mBondDetailRateTitle);

	mBondDetailRateTitle->refreshUi();
	mBondInterestRateChart->loadData(BondDetailCache::instance().getChart(m_data->bond().GetIssuerCode()).m_pInterestIssuer, BondDetailCache::instance().getChart(m_data->bond().GetCombBondKey()).m_pInterestBond);
}

void BondDetailChart::onBondCDCChanged()
{
	QB_CHECK_RETURN_VOID1(m_data && mBondQuoteCDCChart);

	bool bRight = false;
	if (strcmp(m_data->bond().GetOptionType(), "CAL") == 0 ||
		strcmp(m_data->bond().GetOptionType(), "PUT") == 0 ||
		strcmp(m_data->bond().GetOptionType(), "CNP") == 0) {
		bRight = true;
		if (!m_data->bond().IsRight()) {
			bRight = false;
		}
	}

	mBondQuoteCDCChart->setOptionBond(bRight);

	xCDCKlineDayList_c EstimateCurve;
	FIELDCOPY(EstimateCurve.m_bond_key, m_data->bond().GetBondKey());
	FIELDCOPY(EstimateCurve.m_listed_market, m_data->bond().GetListedMarket());

	QString strReqKey = BondUtility::GetReqDataCombKey(&m_data->bond());
	BondAprismtHistoryData* pData = BondDetailCache::instance().getChart(strReqKey.toStdString()).m_pAaprismtHisData;
	if (!pData)
	{
		mBondQuoteCDCChart->loadKLineData(&EstimateCurve);
		return;
	}

	//估值
	std::map<unsigned int, DetailAppraisementPriceData>::iterator itCDC = pData->m_CDCPriceCtner.begin();
	xCDCKlineDayUnit_c cdcDayUnit;
	for (; itCDC != pData->m_CDCPriceCtner.end(); ++itCDC) {
		cdcDayUnit.m_CleanPrice = cdcDayUnit.m_ValueYield = cdcDayUnit.m_DealYield = cdcDayUnit.m_QuoteBid = cdcDayUnit.m_QuoteOfr = cdcDayUnit.m_DealClean = -1000.0f;
		DetailAppraisementPriceData& detailPrice = itCDC->second;
		cdcDayUnit.m_Date = itCDC->first;
		if (!bRight) {
			cdcDayUnit.m_ValueYield = detailPrice.m_RecommendAllData.m_Yield;
			cdcDayUnit.m_CleanPrice = detailPrice.m_RecommendAllData.m_CleanPrice;
		}
		else {
			if (strcmp(detailPrice.m_RecommendAllData.m_cdcExercise, "0") == 0) cdcDayUnit.m_ValueYield = detailPrice.m_RecommendAllData.m_Yield;
			else if (strcmp(detailPrice.m_RecommendAllData.m_cdcExercise, "1") == 0) cdcDayUnit.m_CleanPrice = detailPrice.m_RecommendAllData.m_Yield;

			if (strcmp(detailPrice.m_UnRecommendData.m_cdcExercise, "0") == 0) cdcDayUnit.m_ValueYield = detailPrice.m_UnRecommendData.m_Yield;
			else if (strcmp(detailPrice.m_UnRecommendData.m_cdcExercise, "1") == 0) cdcDayUnit.m_CleanPrice = detailPrice.m_UnRecommendData.m_Yield;
		}
		EstimateCurve.m_List.push_back(cdcDayUnit);
	}

	//成交
	if (m_data->dealKLineDayList()) {
		xCDCKlineDayUnit_c cdcDealUnit;
		std::list<xQBABrokerDealKlineDayUnit_c>::iterator itCDC = m_data->dealKLineDayList()->m_List.begin();

		for (; itCDC != m_data->dealKLineDayList()->m_List.end(); ++itCDC) {
			cdcDealUnit.m_CleanPrice = cdcDealUnit.m_ValueYield = cdcDealUnit.m_DealYield = cdcDealUnit.m_QuoteBid = cdcDealUnit.m_QuoteOfr = cdcDealUnit.m_DealClean = -1000.0f;
			cdcDealUnit.m_Date = itCDC->m_Date;

			std::list<xCDCKlineDayUnit_c>::iterator it = std::find(EstimateCurve.m_List.begin(), EstimateCurve.m_List.end(), cdcDealUnit);

			if (it != EstimateCurve.m_List.end())
			{
				it->m_DealYield = itCDC->m_Yield;
			}
			else
			{
				cdcDealUnit.m_DealYield = itCDC->m_Yield;
				EstimateCurve.m_List.push_back(cdcDealUnit);
			}
		}
	}

	//成交收盘
	if (m_data->dealKLineDayCloseList()) {
		xCDCKlineDayUnit_c cdcDealUnit;
		std::list<xQBABrokerKlineDayCloseUnit_c>::iterator itCDC = m_data->dealKLineDayCloseList()->m_List.begin();
		for (; itCDC != m_data->dealKLineDayCloseList()->m_List.end(); ++itCDC) {
			cdcDealUnit.m_Date = itCDC->m_Date;
			cdcDealUnit.m_DealYield = cdcDealUnit.m_DealClean = cdcDealUnit.m_ValueYield = cdcDealUnit.m_CleanPrice = cdcDealUnit.m_QuoteBid = cdcDealUnit.m_QuoteOfr = -1000.0f;

			std::list<xCDCKlineDayUnit_c>::iterator it = std::find(EstimateCurve.m_List.begin(), EstimateCurve.m_List.end(), cdcDealUnit);
			if (it != EstimateCurve.m_List.end())
			{
				it->m_DealYield = itCDC->m_Yield;
				if (m_iTabDayLineType == 0)
				{
					it->m_DealClean = itCDC->m_AverageClean;
				}
				else if (m_iTabDayLineType == 1)
				{
					it->m_DealClean = itCDC->m_CloseClean;
				}
			}
			else
			{
				cdcDealUnit.m_DealYield = itCDC->m_Yield;
				if (m_iTabDayLineType == 0)
				{
					cdcDealUnit.m_DealClean = itCDC->m_AverageClean;
				}
				else if (m_iTabDayLineType == 1)
				{
					cdcDealUnit.m_DealClean = itCDC->m_CloseClean;
				}
				EstimateCurve.m_List.push_back(cdcDealUnit);
			}
		}
	}

	//报价
	/*if (m_data->boKLineDayCloseList()) {
		xCDCKlineDayUnit_c cdcQuoteUnit;
		std::list<xQBABrokerKlineDayCloseBoUnit_c>::iterator itCDC = m_data->boKLineDayCloseList()->m_List.begin();

		for (; itCDC != m_data->boKLineDayCloseList()->m_List.end(); ++itCDC) {
			cdcQuoteUnit.m_Date = itCDC->m_Date;
			cdcQuoteUnit.m_QuoteBid = cdcQuoteUnit.m_QuoteOfr = cdcQuoteUnit.m_DealYield = cdcQuoteUnit.m_DealClean = cdcQuoteUnit.m_CleanPrice = cdcQuoteUnit.m_ValueYield = -1000.0f;

			std::list<xCDCKlineDayUnit_c>::iterator it = std::find(EstimateCurve.m_List.begin(), EstimateCurve.m_List.end(), cdcQuoteUnit);
			if (it != EstimateCurve.m_List.end())
			{
				it->m_QuoteBid = itCDC->m_BidCloseYield;
				it->m_QuoteOfr = itCDC->m_OfrCloseYield;
			}
			else
			{
				cdcQuoteUnit.m_QuoteBid = itCDC->m_BidCloseYield;
				cdcQuoteUnit.m_QuoteOfr = itCDC->m_OfrCloseYield;
				EstimateCurve.m_List.push_back(cdcQuoteUnit);
			}
		}
	}*/

	mBondQuoteCDCChart->loadKLineData(&EstimateCurve);
}

void BondDetailChart::onMarketStreamDataChanged(QList<int> ids)
{
	QB_CHECK_RETURN_VOID1(m_data && mBondQuoteBrokerChart);

	xQBMarketStreamList_c quoteCurveData;
	m_data->GetChartMktStreamData(quoteCurveData);

	std::list<xQBMarketStreamUnit_c>::const_iterator itr = quoteCurveData.m_List.begin();
	if (itr != quoteCurveData.m_List.end()) {
		if (atoi(itr->m_company_id) == NEW_DETAIL_CFETS_BROKER) {
			if (m_data->getBondType() != em_CFETS) return;
			for (; itr != quoteCurveData.m_List.end();) {
				if (strcmp(itr->m_body1.m_bid_companyname, "XBOND") == 0 || strcmp(itr->m_body1.m_ofr_companyname, "XBOND") == 0) {
					itr = quoteCurveData.m_List.erase(itr);
				}
				else {
					itr++;
				}
			}
		}
		else if (strcmp(itr->m_company_id, "e") == 0) {
			if (m_data->getBondType() != em_Exchange) return;
		}
		else {
			if (m_data->getBondType() == em_CFETS || m_data->getBondType() == em_Exchange)	return;
		}
	}

	mBondQuoteBrokerChart->loadRealTimeData(&quoteCurveData);
}

void BondDetailChart::refreshMarketDealChart()
{
	QB_CHECK_RETURN_VOID1(m_data && mBondQuoteDealChart && mBondRateChart);

	xQBMarketStreamList_c dealCurveData;
	m_data->GetChartMktStreamDealData(dealCurveData);
	mBondQuoteDealChart->loadRealTimeData(&dealCurveData);
	mBondRateChart->loadMktStreamDealData(&dealCurveData);
}

void BondDetailChart::refreshBondDealKLineData()
{
	QB_CHECK_RETURN_VOID1(m_data && mBondQuoteDealChart);

	em_BrokerKinds bondType = (em_BrokerKinds)m_data->getBondType();
	mBondQuoteDealChart->setMarketType(bondType);

	if (0 == m_data->getTabDayLineType())
	{
		if (em_Exchange == bondType)
		{
			mBondQuoteDealChart->loadKLineData(BondDetailCache::instance().getChart(m_data->bond().GetCombBondKey()).m_pDealCurveKLineEx);
		}
		else if (em_CFETS == bondType)
		{
			mBondQuoteDealChart->loadKLineData(BondDetailCache::instance().getChart(m_data->bond().GetCombBondKey()).m_pDealCurveKLineCFETS);
		}
		else
		{
			mBondQuoteDealChart->loadKLineData(BondDetailCache::instance().getChart(m_data->bond().GetCombBondKey()).m_pDealCurveKLineBroker);
		}
	}
	else
	{
		if (em_Exchange == bondType)
		{
			mBondQuoteDealChart->loadKLineCloseData(BondDetailCache::instance().getChart(m_data->bond().GetCombBondKey()).m_pDealKlineDayCloseEx);
		}
		else if (em_CFETS == bondType)
		{
			mBondQuoteDealChart->loadKLineCloseData(BondDetailCache::instance().getChart(m_data->bond().GetCombBondKey()).m_pCfetsDealKlineCfetsDayClose);
		}
		else
		{
			mBondQuoteDealChart->loadKLineCloseData(BondDetailCache::instance().getChart(m_data->bond().GetCombBondKey()).m_pBrokerDealKlineBrokerDayClose);
		}
	}
}

void BondDetailChart::refreshYeildAndIntChart()
{
	QB_CHECK_RETURN_VOID1(m_data);

	if (m_data->isShowYieldChart())
	{
		refreshYeildChart();
	}
	else
	{
		refreshIntChart();
	}
}

void BondDetailChart::refreshYeildChart()
{
	QB_CHECK_RETURN_VOID1(mBondRateChart && mBondInterestRateChart);

	mBondInterestRateChart->setVisible(false);
	mDetailLayout[2]->replaceWidget(mBondInterestRateChart, mBondRateChart);
	mBondRateChart->setVisible(true);
}

void BondDetailChart::refreshIntChart()
{
	QB_CHECK_RETURN_VOID1(mBondRateChart && mBondInterestRateChart && m_data);

	mBondRateChart->setVisible(false);
	mDetailLayout[2]->replaceWidget(mBondRateChart, mBondInterestRateChart);
	mBondInterestRateChart->setVisible(true);
	mBondInterestRateChart->setMarketType((em_BrokerKinds)m_data->getBondType());
	mBondInterestRateChart->setWndType(m_data->getWndType());
}

void BondDetailChart::onBondDealTabSelRealTime()
{
	QB_CHECK_RETURN_VOID1(m_data && mBondQuoteDealChart);

	mBondQuoteDealChart->resetData();
	mBondQuoteDealChart->setMarketType((em_BrokerKinds)m_data->getBondType());
	mBondQuoteDealChart->setSelect(1);
	m_data->reqDealChartData();
	refreshMarketDealChart();
}

void BondDetailChart::onBondDealTabSelKLineAver()
{
	QB_CHECK_RETURN_VOID1(m_data && mBondQuoteDealChart);

	mBondQuoteDealChart->resetData();
	mBondQuoteDealChart->setMarketType((em_BrokerKinds)m_data->getBondType());
	mBondQuoteDealChart->setSelect(0);
	m_data->setTabDayLineType(0);
	m_data->reqDealChartData();
}

void BondDetailChart::onBondDealTabSelKLineClose()
{
	QB_CHECK_RETURN_VOID1(m_data && mBondQuoteDealChart);

	mBondQuoteDealChart->resetData();
	mBondQuoteDealChart->setMarketType((em_BrokerKinds)m_data->getBondType());
	mBondQuoteDealChart->setSelect(0);
	m_data->setTabDayLineType(1);
	m_data->reqDealChartData();
}

void BondDetailChart::onBondDealTabSelDate(int index)
{
	QB_CHECK_RETURN_VOID1(m_data && mBondQuoteDealChart);

	mBondQuoteDealChart->resetData();
	mBondQuoteDealChart->setDateSel(index);
	mBondQuoteDealChart->setSelect(0);

	refreshBondDealKLineData();
}

void BondDetailChart::onBondDealCalendarUpdate(int startDay, int endDay)
{
	QB_CHECK_RETURN_VOID1(m_data && mBondQuoteDealChart);

	mBondQuoteDealChart->resetData();
	mBondQuoteDealChart->setCalendar(startDay, endDay);
	mBondQuoteDealChart->setSelect(0);

	refreshBondDealKLineData();
}

void BondDetailChart::onQuoteYieldSelected()
{
	QB_CHECK_RETURN_VOID1(mBondQuoteBrokerChart);

	mBondQuoteBrokerChart->setMarketType((em_BrokerKinds)m_data->getBondType());
	mBondQuoteBrokerChart->setPriceType(BondQuoteBrokerChart::prc_Yeild);
	onMarketStreamDataChanged(QList<int>());
}

void BondDetailChart::onQuoteNetPriceSelected()
{
	QB_CHECK_RETURN_VOID1(mBondQuoteBrokerChart);

	mBondQuoteBrokerChart->setMarketType((em_BrokerKinds)m_data->getBondType());
	mBondQuoteBrokerChart->setPriceType(BondQuoteBrokerChart::prc_Clean);
	onMarketStreamDataChanged(QList<int>());
}

void BondDetailChart::onQuoteAbValueSelected(int check)
{
	QB_CHECK_RETURN_VOID1(mBondQuoteBrokerChart);
	mBondQuoteBrokerChart->setAbCheck(check);
}

void BondDetailChart::onBondCreditSpreadSelected(int select)
{
	QB_CHECK_RETURN_VOID1(mBondInterestRateChart && m_data);

	refreshIntChart();
	mBondInterestRateChart->setSelect(select);
	onBondInterestChanged();
}

void BondDetailChart::onBondYieldSelected(int select)
{
	QB_CHECK_RETURN_VOID1(mBondRateChart);

	refreshYeildChart();
	mBondRateChart->setType((BondRateChart::emtype)select);
	onBondRateChanged();
}

void BondDetailChart::onBondCDCYieldSelected(int check0, int check1)
{//check0:估值,check1:成交
	QB_CHECK_RETURN_VOID1(mBondQuoteCDCChart);

	mBondQuoteCDCChart->setTab(0);
	mBondQuoteCDCChart->setCheck(0, check0);
	mBondQuoteCDCChart->setCheck(1, check1);
	//mBondQuoteCDCChart->setCheck(2, check2); 报价
	onBondCDCChanged();
}

void BondDetailChart::onBondCDCNetPriceSelected(int check0, int check1)
{
	QB_CHECK_RETURN_VOID1(mBondQuoteCDCChart);

	mBondQuoteCDCChart->setTab(1);
	mBondQuoteCDCChart->setCheck(0, check0);
	mBondQuoteCDCChart->setCheck(1, check1);
//	mBondQuoteCDCChart->setCheck(2, check2);
	onBondCDCChanged();
}

void BondDetailChart::onBondCDCDateTabSelected(int select)
{
	QB_CHECK_RETURN_VOID1(mBondQuoteCDCChart);

	mBondQuoteCDCChart->setDateSel(select);
	onBondCDCChanged();
}

void BondDetailChart::onBondCDCCalendarUpdate(int startDay, int endDay)
{
	QB_CHECK_RETURN_VOID1(mBondQuoteCDCChart);

	mBondQuoteCDCChart->setCalendar(startDay, endDay);
	onBondCDCChanged();
}

void BondDetailChart::onBondDealLeftDrag(double ratio)
{
	QB_CHECK_RETURN_VOID1(mBondQuoteDealChart);

	mBondQuoteDealChart->setLeftDrag(ratio);

	if (0 == mBondQuoteDealChart->getSelect())
	{
		refreshBondDealKLineData();
	}
	else
	{
		refreshMarketDealChart();
	}
}

void BondDetailChart::onBondDealRightDrag(double ratio)
{
	QB_CHECK_RETURN_VOID1(mBondQuoteDealChart);

	mBondQuoteDealChart->setRightDrag(ratio);

	if (0 == mBondQuoteDealChart->getSelect())
	{
		refreshBondDealKLineData();
	}
	else
	{
		refreshMarketDealChart();
	}
}

void BondDetailChart::onBondDealCentralDrag(double ratio1, double ratio2)
{
	QB_CHECK_RETURN_VOID1(mBondQuoteDealChart);

	mBondQuoteDealChart->setCentralDrag(ratio1, ratio2);

	if (0 == mBondQuoteDealChart->getSelect())
	{
		refreshBondDealKLineData();
	}
	else
	{
		refreshMarketDealChart();
	}
}

void BondDetailChart::onChartDblClicked(QWidget* e)
{
	for (auto l : mDetailLayout)
	{
		if (l->indexOf((QWidget*)e) == -1)
			showLayout(l, m_chartExpand);
	}

	m_chartExpand = !m_chartExpand;
}

void BondDetailChart::onBondQuoteLeftDrag(double ratio)
{
	QB_CHECK_RETURN_VOID1(mBondQuoteBrokerChart);

	mBondQuoteBrokerChart->setLeftDrag(ratio);
	onMarketStreamDataChanged(QList<int>());
}

void BondDetailChart::onBondQuoteRightDrag(double ratio)
{
	QB_CHECK_RETURN_VOID1(mBondQuoteBrokerChart);

	mBondQuoteBrokerChart->setRightDrag(ratio);
	onMarketStreamDataChanged(QList<int>());
}

void BondDetailChart::onBondQuoteCentralDrag(double ratio1, double ratio2)
{
	QB_CHECK_RETURN_VOID1(mBondQuoteBrokerChart);

	mBondQuoteBrokerChart->setCentralDrag(ratio1, ratio2);
	onMarketStreamDataChanged(QList<int>());
}

void BondDetailChart::onBondRateLeftDrag(double ratio)
{
	QB_CHECK_RETURN_VOID1(mBondRateChart);

	mBondRateChart->setLeftDrag(ratio);
	onBondRateChanged();
}

void BondDetailChart::onBondRateRightDrag(double ratio)
{
	QB_CHECK_RETURN_VOID1(mBondRateChart);

	mBondRateChart->setRightDrag(ratio);
	onBondRateChanged();
}

void BondDetailChart::onBondRateCentralDrag(double ratio1, double ratio2)
{
	QB_CHECK_RETURN_VOID1(mBondRateChart);

	mBondRateChart->setCentralDrag(ratio1, ratio2);
	onBondRateChanged();
}

void BondDetailChart::onBondInterestLeftDrag(double ratio)
{
	QB_CHECK_RETURN_VOID1(mBondInterestRateChart);

	mBondInterestRateChart->setLeftDrag(ratio);
	onBondInterestChanged();
}

void BondDetailChart::onBondInterestRightDrag(double ratio)
{
	QB_CHECK_RETURN_VOID1(mBondInterestRateChart);

	mBondInterestRateChart->setRightDrag(ratio);
	onBondInterestChanged();
}

void BondDetailChart::onBondInterestCentralDrag(double ratio1, double ratio2)
{
	QB_CHECK_RETURN_VOID1(mBondInterestRateChart);

	mBondInterestRateChart->setCentralDrag(ratio1, ratio2);
	onBondInterestChanged();
}

void BondDetailChart::onBondCDCLeftDrag(double ratio)
{
	QB_CHECK_RETURN_VOID1(mBondQuoteCDCChart);

	mBondQuoteCDCChart->setLeftDrag(ratio);
	onBondCDCChanged();
}

void BondDetailChart::onBondCDCRightDrag(double ratio)
{
	QB_CHECK_RETURN_VOID1(mBondQuoteCDCChart);

	mBondQuoteCDCChart->setRightDrag(ratio);
	onBondCDCChanged();
}

void BondDetailChart::onBondCDCCentralDrag(double ratio1, double ratio2)
{
	QB_CHECK_RETURN_VOID1(mBondQuoteCDCChart);

	mBondQuoteCDCChart->setCentralDrag(ratio1, ratio2);
	onBondCDCChanged();
}
